﻿using System;
using System.Collections.Generic;
using Looptribe.Moodlin8.Model;
using Looptribe.Moodlin8.ViewModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

// The Grouped TopItems Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234231

namespace Looptribe.Moodlin8.View
{
    /// <summary>
    /// A page that displays a grouped collection of items.
    /// </summary>
    public sealed partial class CoursePage : Looptribe.Moodlin8.Common.LayoutAwarePage
    {
        public CoursePage()
        {
            this.InitializeComponent();
        }

        public CourseViewModel ViewModel { get { return (CourseViewModel) DataContext; } }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected async override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            if (navigationParameter is Course)
                await ViewModel.LoadContent((Course) navigationParameter);
            else if (navigationParameter is long) // Entry point for secondary tiles
                await ViewModel.LoadContent((long) navigationParameter);
        }

        /*private void ItemGridViewOnItemClick(object sender, ItemClickEventArgs e)
        {
            var courseContentWrapper = e.ClickedItem as CourseContentWrapper;
            if (courseContentWrapper != null)
            {
                if (ViewModel.GotoCourseContentCommand.CanExecute(courseContentWrapper.Content))
                    ViewModel.GotoCourseContentCommand.Execute(courseContentWrapper.Content);
            }
        }*/

        private void CourseFilesItem_Clicked(object sender, ItemClickEventArgs e)
        {
            var wrapper = e.ClickedItem as ModuleContentWrapper;
            if (wrapper == null)
                return;

            if (ViewModel.ModuleContentCommand.CanExecute(wrapper.Content))
                ViewModel.ModuleContentCommand.Execute(wrapper.Content);

            //Windows.System.Launcher.LaunchUriAsync(new Uri(wrapper.Content.FileUrl));

            /*var selectedFile = e.ClickedItem as StorageFile;
            if (selectedFile == null) return;
            await Windows.System.Launcher.LaunchFileAsync(selectedFile);*/
        }

        private void TeacherClick(object sender, ItemClickEventArgs e)
        {
            if (ViewModel.GotoTeacherCommand.CanExecute(e.ClickedItem))
                ViewModel.GotoTeacherCommand.Execute(e.ClickedItem);
        }

        private void PinClick(object sender, RoutedEventArgs e)
        {
            if (ViewModel.PinToStartCommand.CanExecute(Pin))
                ViewModel.PinToStartCommand.Execute(Pin);
        }
    }
}
